package tester;

import java.awt.EventQueue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.IOException;
import java.net.*;

public class TesterGUI extends JFrame implements ActionListener{

        private JFrame frmEspServerApplication;
        private JTextField portField;
        private JTextArea taLog = new JTextArea();
        private JScrollPane scrollPane = new JScrollPane();
        private JButton btnStart = new JButton("Start");
        private JTextField sendField;
        private JButton btnSend = new JButton("Send");
		private boolean connectionStatus = false;
		private Tester tester;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        TesterGUI window = new TesterGUI();
                                        window.frmEspServerApplication.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the application.
         */
        public TesterGUI() {
                initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
                //This sets up the application window (JFRAME)
                frmEspServerApplication = new JFrame();
                frmEspServerApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frmEspServerApplication.setTitle("ESP Tester Application");
                frmEspServerApplication.setBounds(100, 100, 600, 300);
                frmEspServerApplication.getContentPane().setLayout(null);
               
                JTabbedPane tabPane = new JTabbedPane(JTabbedPane.TOP);
                tabPane.setBounds(10, 11, 564, 240);
                frmEspServerApplication.getContentPane().add(tabPane);
               
                JPanel pnlMain = new JPanel();
                tabPane.addTab("Main Tab", null, pnlMain, null);
               
                //Adds the port text field
                portField = new JTextField();
                portField.setText("4444");
                portField.setBounds(64, 11, 108, 20);
                portField.setColumns(10);
                pnlMain.setLayout(null);
               
                //Adds the Labeling for the Port field
                JLabel lblPort = new JLabel("Port");
                lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
                lblPort.setBounds(10, 14, 44, 14);
                pnlMain.add(lblPort);
                pnlMain.add(portField);
               
                //Adds the Start Button
                btnStart = new JButton("Start");
                btnStart.setToolTipText("Starts the server");
                btnStart.addActionListener(this);
                btnStart.setBounds(169, 189, 89, 23);
                pnlMain.add(btnStart);
               
                //Adds the log Panel
                JPanel pnlLog = new JPanel();
                pnlLog.setLayout(null);
                tabPane.addTab("Log", null, pnlLog, null);
               
                scrollPane = new JScrollPane();
                scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.setBounds(10, 11, 539, 177);
                pnlLog.add(scrollPane);
                scrollPane.setViewportView(taLog);
                
                btnSend.setBounds(10, 189, 89, 23);
                btnSend.addActionListener(this);
                pnlLog.add(btnSend);
                
                sendField = new JTextField();
                sendField.addActionListener(this);
                sendField.setBounds(109, 190, 440, 20);
                pnlLog.add(sendField);
                sendField.setColumns(10);
        }
        
        public void log(String msg){
            taLog.append(msg);
        }
               
        @Override
        public void actionPerformed(ActionEvent a) {
                if (a.getSource() == btnStart && (!connectionStatus)){
                        try {
                                tester = new Tester(this, Integer.parseInt(portField.getText()));
                                connectionStatus = true;
                        } catch (NumberFormatException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                
                if ( (a.getSource() == sendField || a.getSource() == btnSend) && connectionStatus){
        			tester.sendMessage(sendField.getText());
        			sendField.setText("");
        		}
               
        }
}

